package com.wanxi.Internet;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class Server {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;

        OutputStream outputStream =null;
        try {
            int port = 1029;
            serverSocket = new ServerSocket(port);

            socket = serverSocket.accept();

            inputStream = socket.getInputStream();


            File file =new File("dsbfh.jpg");
            fileOutputStream = new FileOutputStream(file);
            byte [] buffer = new byte[1024];
            int len;
            while ((len= inputStream.read(buffer))!=-1){
                fileOutputStream.write(buffer,0,len);
            }
            System.out.println("数据接收完毕");

           outputStream = socket.getOutputStream();
            outputStream.write("已经接收到你的图片了".getBytes(StandardCharsets.UTF_8));


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }

        try {
            if(fileOutputStream!=null)
                fileOutputStream.close();
            if(inputStream!=null)
                inputStream.close();
            if ((socket != null))
                socket.close();
            if(serverSocket!=null)
                serverSocket.close();

            if (outputStream!=null)
                outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
